﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using HROReporting.Infrastructure.Authentication;
using HROReporting.Controllers.ActionArguments;
using HROReporting.Controllers.ViewModels.Account;
using HROReporting.Services.Interfaces;
using HROReporting.Services.Implementations;
using HROReporting.Services.Messaging.StudentService;

namespace HROReporting.Controllers.Controllers
{
    public class StudentRegisterController : BaseAccountController
    {
        public StudentRegisterController(ILocalAuthenticationService authenticationService,
                                         IStudentService studentService,
                                         IFormsAuthentication formsAuthentication,
                                         IActionArguments actionArguments)
            : base(authenticationService,
                   studentService,
                   formsAuthentication,
                   actionArguments)
        {
        }

        public ActionResult Register()
        {
            AccountView accountView = InitializeAccountViewWithIssue(false, string.Empty);

            return View(accountView);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(FormCollection collection)
        {
            User user;

            string password = collection[FormDataKeys.Password.ToString()];
            string email = collection[FormDataKeys.Email.ToString()];

            try
            {
                user = _authenticationService.RegisterUser(email, password, "student");
            }
            catch (InvalidOperationException ex)
            {
                AccountView accountView = InitializeAccountViewWithIssue(true, ex.Message);

                return View(accountView);
            }

            if (user.IsAuthenticated)
            {
                try
                {
                    CreateStudentRequest createStudentRequest = new CreateStudentRequest();
                    createStudentRequest.StudentIdentityToken = user.AuthenticationToken;
                    createStudentRequest.Email = email;

                    _formsAuthentication.SetAuthenticationToken(email);
                    _studentService.CreateStudent(createStudentRequest);

                    return RedirectToAction("Details", "Student");
                }
                catch (StudentInvalidException ex)
                {
                    AccountView accountView = InitializeAccountViewWithIssue(true, ex.Message);

                    return View(accountView);
                }
            }
            else
            {
                AccountView accountView = InitializeAccountViewWithIssue(true, "We kunnen u helaas niet authenticeren. " + "Probeer het nog eens.");

                return View(accountView);
            }
        }

        private AccountView InitializeAccountViewWithIssue(bool hasIssue, string message)
        {
            AccountView accountView = new AccountView();
            accountView.HasIssue = hasIssue;
            accountView.Message = message;

            return accountView;
        }
    }
}
